package ldkxingzhe.ldkxingzhedemo.view;

import android.app.Activity;
import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import ldkxingzhe.ldkxingzhedemo.R;


/**
 * Created by ke on 15-8-20.
 */
public class DragListenerActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag_listener);

        MyTouchListener myTouchListener = new MyTouchListener();
        findViewById(R.id.my_image1).setOnTouchListener(myTouchListener);
        findViewById(R.id.my_image2).setOnTouchListener(myTouchListener);
        findViewById(R.id.my_image3).setOnTouchListener(myTouchListener);
        findViewById(R.id.my_image4).setOnTouchListener(myTouchListener);
        MyDragListener myDragListener = new MyDragListener();
        findViewById(R.id.bottom_left).setOnDragListener(myDragListener);
        findViewById(R.id.top_left).setOnDragListener(myDragListener);
        findViewById(R.id.bottom_right).setOnDragListener(myDragListener);
        findViewById(R.id.top_right).setOnDragListener(myDragListener);
    }

    class MyDragListener implements View.OnDragListener{

        Drawable enterShape = getResources().getDrawable(R.drawable.shape_dragtargent);
        Drawable normalShape = getResources().getDrawable(R.drawable.shape);
        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
//            Log.d("TAG","onDrag");
            switch (action){
                case DragEvent.ACTION_DRAG_STARTED:
//                    Log.d("TAG","ACTION_DRAG_STARTED");
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
//                    Log.d("TAG","ACTION_DRAG_ENTERED");
//                    Log.d("TAG","v.getId = " + v.getId());
                    v.setBackgroundDrawable(enterShape);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setBackgroundDrawable(normalShape);
//                    Log.d("TAG","ACTION_DRAG_EXITED, v.getId = " + v.getId());
                    break;
                case DragEvent.ACTION_DROP:
                    View view = (View)event.getLocalState();
                    ViewGroup owner = (ViewGroup)view.getParent();
                    owner.removeView(view);
                    LinearLayout container = (LinearLayout)v;
                    container.addView(view);
                    view.setVisibility(View.VISIBLE);
//                    Log.d("TAg","view.getId = " + view.getId() + "\t owner.getId = "
//                            + owner.getId() + "container.getId = " + container.getId());
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    v.setBackgroundDrawable(normalShape);
//                    Log.d("TAG", "ACTION_DRAG_ENDED, v.getId = " + v.getId());
                    break;

            }
            return true;
        }
    }
}

final class MyTouchListener implements View.OnTouchListener{

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            ClipData data = ClipData.newPlainText("","");
            View.DragShadowBuilder builder = new View.DragShadowBuilder(v);
            v.startDrag(data, builder, v, 0);
            Log.d("TAg", "Here, after startDrag");
            v.setVisibility(View.INVISIBLE);
            return true;
        }else{
            Log.d("TAG","onTouch return false");
            return false;
        }
    }
}


